package com.k52group.vn.micromarket.adapter;

import java.util.List;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.text.Html;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.k52group.vn.micromarket.R;
import com.k52group.vn.micromarket.classes.Profile;
import com.k52group.vn.micromarket.utils.Constant;
import com.k52group.vn.micromarket.utils.HttpRequest;
import com.k52group.vn.micromarket.utils.Utils;

public class HitAdapter extends BaseAdapter {
	private List<Profile> list;
	private LayoutInflater mInflater;
	private Context context;
	private boolean isGuest;

	public HitAdapter(Context context, List<Profile> list) {
		this.list = list;
		this.mInflater = LayoutInflater.from(context);
		this.context = context;
	}

	public boolean isGuest() {
		return isGuest;
	}

	public void setGuest(boolean isGuest) {
		this.isGuest = isGuest;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return list.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.row_hit, null);
			holder = new ViewHolder();
			holder.tvsendmessage = (Button) convertView.findViewById(R.id.TextView04);
			holder.tvdistance = (TextView) convertView.findViewById(R.id.TextView02);
			holder.tvservice = (TextView) convertView.findViewById(R.id.TextView01);
			holder.tvphone = (TextView) convertView.findViewById(R.id.textView2);
			holder.tvabout = (TextView) convertView.findViewById(R.id.textView1);
			holder.tvcompany = (TextView) convertView.findViewById(R.id.TextView06);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		final Profile item = list.get(position);
		holder.tvdistance.setText(item.getDistance(context));
		holder.tvservice.setText(item.getService());
		holder.tvphone.setText("");
		try {
			holder.tvabout.setText(item.getAbout_your_service().trim());
		} catch (Exception e) {
			holder.tvabout.setText("");
		}
		holder.tvcompany.setText(Html.fromHtml("<b>" + item.getYour_company() + " </b>  " + item.getPhone()));
		holder.tvsendmessage.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				confirmSendMessage(item.getUserId() + "", item.getGcm_regid());

			}
		});
		// if (isGuest) {
		// holder.tvsendmessage.setVisibility(View.INVISIBLE);
		// } else {
		// holder.tvsendmessage.setVisibility(View.VISIBLE);
		// }

		return convertView;
	}

	private static class ViewHolder {
		private TextView tvdistance, tvservice, tvphone, tvabout, tvcompany;
		Button tvsendmessage;
	}

	private void confirmSendMessage(final String reciver, final String gcm) {
		AlertDialog.Builder dialogDelete = new AlertDialog.Builder(context);
		dialogDelete.setTitle(Constant.NAME_APP);
		final EditText et = new EditText(context);
		et.setHint("Message");
		et.setSingleLine(false);
		et.setGravity(Gravity.TOP);
		et.setHeight(160);
		dialogDelete.setView(et);
		Profile profile = Utils.profile(context);
		final String sender = profile.getUserId() + "";

		dialogDelete.setPositiveButton("Send", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				new SendMessageTask(sender, reciver, et.getText().toString(), gcm).execute();
			}
		});
		dialogDelete.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
			}
		});
		dialogDelete.setIcon(R.drawable.ic_launcher);
		dialogDelete.show();
	}

	private class SendMessageTask extends AsyncTask<String, String, String> {
		ProgressDialog dialog;
		String sender;
		String reciever;
		String message;
		String gcm;

		public SendMessageTask(String sender, String reciever, String message, String gcm) {
			// TODO Auto-generated constructor stub
			this.message = message;
			this.sender = sender;
			this.reciever = reciever;
			this.gcm = gcm;
		}

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			dialog = new ProgressDialog(context);
			dialog.setMessage("Sending ...");
			dialog.show();
		}

		@Override
		protected String doInBackground(String... arg0) {
			// TODO Auto-generated method stub
			HttpRequest request = HttpRequest.post(Constant.URL_SEND_MESSAGE);

			request.form("sender", sender);
			request.form("reciever", reciever);
			request.form("msg", message);
			request.form("regId", gcm);
			System.out.println("body" + request.body());
			return null;
		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			dialog.dismiss();
			Toast.makeText(context, "Sent Message", Toast.LENGTH_SHORT).show();
		}
	}
}
